﻿import flash.geom.Point;
import flash.geom.Matrix;
class com.embedchat.skins.standart.scrollbar.ScrollBarButtonClass extends MovieClip{
	static var instanceName:String = new String ("ScrollBarButtonClass");
	static var instanceOwner:Object = com.embedchat.skins.standart.scrollbar.ScrollBarButtonClass;
	[Inspectable(name="Arrow direction", defaultValue="none",type:String, enumeration="none,up,down,left,right")]
	public var arrawDirection:String;
	[Inspectable(name="Arrow length", defaultValue=3,type:Number]
	public var arrowCircleRadius:Number;	
	[Inspectable(name="Button width",defaultValue=16,type:Number)]
	public var canvasWidth:Number;
	[Inspectable(name="Button height",defaultValue=17,type:Number)]	
	public var canvasHeight:Number;
	public var canvasCurveRadius:Number = 2;	
	private var broadcasterObj:Object = null;
	private var scrollMessageIntervalObject:Number = -1;
	private var scrollMessageInterval:Number = 100; //ms	
	
	function ScrollBarButtonClass(){
		this.useHandCursor = false;
		this.onRelease = this.onMouseOutEvent;
		this.onReleaseOutside = this.onMouseOutEvent;
		this.onRollOut = this.onMouseOutEvent;
		this.onRollOver = this.onMouseOverEvent;
		this.onPress = this.onMouseDownEvent;
		this.onMouseOutEvent();
	}
	
	private function onMouseOutEvent():Void{
		//logic
		if(this.scrollMessageIntervalObject != -1){
			clearInterval(this.scrollMessageIntervalObject);
			this.scrollMessageIntervalObject = -1
		}		
		//gui
		this.clear();
		var fillType:String = "linear"
		var colors:Array = [0xFFFFFF, 0xBCD0E8];
		var alphas:Array = [100, 100];
		var ratios:Array = [0x00, 0xFF];
		var matrix:Matrix = new Matrix();
		matrix.createGradientBox(this.canvasWidth, this.canvasHeight, 0.5*Math.PI, 0, -2);
		var spreadMethod:String = "pad";
		
		this.lineStyle(0, 0xA4B8D0, 100, true, "none", "round", "miter", 1);		
		this.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
		
		this.drawCanvas();
		
		this.endFill();
		
		this.lineStyle(3, 0x6B7B84, 100, true, "none", "round", "miter", 1);		
		this.drawArrow();
	}
	
	private function onMouseOverEvent():Void{
		//gui		
		this.clear();
		var fillType:String = "linear"
		var colors:Array = [0xFFFFFF, 0xBCD0E8];
		var alphas:Array = [100, 100];
		var ratios:Array = [0x00, 0xFF];
		var matrix:Matrix = new Matrix();
		matrix.createGradientBox(this.canvasWidth, this.canvasHeight, 0.5*Math.PI, 0, 2);
		var spreadMethod:String = "pad";
		
		this.lineStyle(0, 0x52667E, 100, true, "none", "round", "miter", 1);		
		this.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
		
		this.drawCanvas();
		
		this.endFill();
		
		this.lineStyle(3, 0x6B7B84, 100, true, "none", "round", "miter", 1);		
		this.drawArrow();
	}
	private function onMouseDownEvent():Void{
		//logic
		this.createBroadcastMessage("onScrollButtonPress");
		if(this.scrollMessageIntervalObject == -1){
			this.scrollMessageIntervalObject = setInterval(this,"createBroadcastMessage",this.scrollMessageInterval,"onScrollButtonPress");
		}
		//gui
		this.clear();
		var fillType:String = "linear"
		var colors:Array = [0xBCD0E8,0xFFFFFF];
		var alphas:Array = [100, 100];
		var ratios:Array = [0x00, 0xFF];
		var matrix:Matrix = new Matrix();
		matrix.createGradientBox(this.canvasWidth, this.canvasHeight, 0.5*Math.PI, 0, -2);
		var spreadMethod:String = "pad";
		
		this.lineStyle(0, 0x52667E, 100, true, "none", "round", "miter", 1);		
		this.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
		
		this.drawCanvas();
		
		this.endFill();
		
		this.lineStyle(3, 0x6B7B84, 100, true, "none", "round", "miter", 1);		
		this.drawArrow();
	}	
	
	private function drawArrow():Void {
		var basePoint:Point = new Point(this.canvasWidth/2,this.canvasHeight/2);//center of the arrow
		switch (this.arrawDirection) {
		case "up":
			this.moveTo(basePoint.x, basePoint.y-this.arrowCircleRadius);
			this.lineTo(basePoint.x+this.arrowCircleRadius, basePoint.y);
			this.moveTo(basePoint.x, basePoint.y-this.arrowCircleRadius);
			this.lineTo(basePoint.x-this.arrowCircleRadius, basePoint.y);
			break;
		case "down":
			this.moveTo(basePoint.x, basePoint.y+this.arrowCircleRadius);
			this.lineTo(basePoint.x+this.arrowCircleRadius, basePoint.y);
			this.moveTo(basePoint.x, basePoint.y+this.arrowCircleRadius);
			this.lineTo(basePoint.x-this.arrowCircleRadius, basePoint.y);
			break;
		case "left" :
			this.moveTo(basePoint.x-this.arrowCircleRadius, basePoint.y);
			this.lineTo(basePoint.x, basePoint.y+this.arrowCircleRadius);
			this.moveTo(basePoint.x-this.arrowCircleRadius, basePoint.y);
			this.lineTo(basePoint.x, basePoint.y-this.arrowCircleRadius);
			break;
		case "right" :
			this.moveTo(basePoint.x+this.arrowCircleRadius, basePoint.y);
			this.lineTo(basePoint.x, basePoint.y+this.arrowCircleRadius);
			this.moveTo(basePoint.x+this.arrowCircleRadius, basePoint.y);
			this.lineTo(basePoint.x, basePoint.y-this.arrowCircleRadius);		
			break;
		default:
			break;
		}
	}
	private function drawCanvas():Void{
		this.moveTo(this.canvasCurveRadius, 0);
		this.lineTo(this.canvasWidth-this.canvasCurveRadius, 0);
		this.curveTo(this.canvasWidth, 0, this.canvasWidth, this.canvasCurveRadius);
		this.lineTo(this.canvasWidth, this.canvasHeight-this.canvasCurveRadius);
		this.curveTo(this.canvasWidth, this.canvasHeight, this.canvasWidth-this.canvasCurveRadius, this.canvasHeight);
		this.lineTo(this.canvasCurveRadius, this.canvasHeight);
		this.curveTo(0, this.canvasHeight, 0, this.canvasHeight-this.canvasCurveRadius);
		this.lineTo(0, this.canvasCurveRadius);
		this.curveTo(0, 0, this.canvasCurveRadius, 0);
	}
	
	private function createBroadcastMessage(eventName:String):Void{
		if(this.broadcasterObj){
			this.broadcasterObj.broadcastMessage(eventName,this);
		}
	}
}